package test.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;
import test.service.RedisReceiver;

@Configuration
public class RedisConfig {

    //初始化监听器
    @Bean
    RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory,
                                            MessageListenerAdapter listenerAdapter,
                                            MessageListenerAdapter two) {
        RedisMessageListenerContainer container = new RedisMessageListenerContainer();

        container.setConnectionFactory(connectionFactory);
        // 每条消息都需要从相同或不同的管道进入，此处只配置一个管道，如果需要多个消息订阅处理，则需要配置多个
        container.addMessageListener(listenerAdapter, new PatternTopic("channel1"));

        container.addMessageListener(two, new PatternTopic("channel_2"));
        return container;
    }


    @Bean
    MessageListenerAdapter listenerAdapter(RedisReceiver redisReceiver) {
        /**
         * 指定的对象，RedisReceiver
         * 指定对象中的指定处理方法 xiangjiao 对应 RedisReceiver 类中的 xiangjiao(String message)
         */
        return new MessageListenerAdapter(redisReceiver, "xiangjiao");
    }


    @Bean
    MessageListenerAdapter two(RedisReceiver redisReceiver) {
        /**
         * 指定的对象，RedisReceiver
         * 指定对象中的指定处理方法 xiangjiao_2 对应 RedisReceiver 类中的 xiangjiao_2(String message)
         */
        return new MessageListenerAdapter(redisReceiver, "xiangjiao_2");
    }
}
